package cn.com.dhc.chesishanbigdemo1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class RegisterDao {

	// JDBC driver name and database URL
	static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
	static final String DB_URL = "jdbc:mysql://localhost/cssdemo";

	// Database credentials
	static final String USER = "root";
	static final String PASS = "root";

	/**
	 * Thread
	 */
	public RegisterDao() {
		try {
			Class.forName(JDBC_DRIVER);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public int insertUser(User user) {
		String username = user.getUsername();
		String password = user.getPswd();

		try {
			Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
			String sql = "insert into userinfo(username, pswd) values (?, ?)";
			PreparedStatement prepareStatement = conn.prepareStatement(sql);
			prepareStatement.setString(1, username);
			prepareStatement.setString(2, password);
			int influenceCount = prepareStatement.executeUpdate();
			return influenceCount;
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
		
		// select
		// return User (ResultSet)
	}
}
